package com.example.lib_base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
 * B-数据类
 * VH-自己实现的：RecyclerView.ViewHolder
 */
abstract class BaseRecyclerviewAdapter<B,C:ViewDataBinding>(val datasList:ArrayList<B>, val layoutId:Int): RecyclerView.Adapter<BaseRecyclerviewAdapter.BaseViewHolder<C, B>>() {

    abstract class BaseViewHolder<C: ViewDataBinding, B>(binding:C): RecyclerView.ViewHolder(binding.root){
        abstract fun bind(item:B, position:Int)
    }

    protected abstract fun createViewHolder(view: C): BaseViewHolder<C, B>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<C, B> {
        val inflate = DataBindingUtil.inflate<C>(LayoutInflater.from(parent.context), layoutId, parent, false)
        return createViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return datasList.size
    }

    override fun onBindViewHolder(holder: BaseViewHolder<C, B>, position: Int) {
        holder.bind(datasList[position], position)
    }

}

